#include "parser.h"
#define printf pspDebugScreenPrintf

void parser(char* str)
{
	char *key=(char*)malloc(5*sizeof(char));
	char *posx=(char*)malloc(3*sizeof(char));
	char *posy=(char*)malloc(3*sizeof(char));
	char *angle=(char*)malloc(3*sizeof(char));
	int parsed = 0;
	int i = 0;
	int j =0;//int parsed = 1;
	//printf("parsing\n");
	SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
	if (strlen(str)>8)
	{
		/* p:000!\
		 * e:221:116:000!\
		 * e:221:116:000!\
		 * e:221:116:000!\
		 * e:221:116:000!\
		 * e:221:116:000!\
		 * e:220:110:000!\
		 * e:183:149:000!\
		 * f:000:000!\
		 * *******************************/

		while (parsed != 1)
		{
			/****************  Player ***********************/
			if (str[i]=='p')
			{
				i+=2;
				j = 0;
				while (j<3)
				{
					angle[j]=str[i];
					i++;
					j++;
					//parsed = 1;
				}
				player->viewAngle=atoi(angle);
				//drawPlayer(player);
				i++;
			}
			/****************  Ennemy ***********************/
			if (str[i]=='e')
			{
				//printf("I'v got an ennemy\n");
				i+=2;
				j =0;
				while (j<3)
				{
					posx[j]=str[i];
					i++;
					j++;
					//parsed = 1;
				}
				i++;
				j=0;
				while (j<3)
				{
					posy[j]=str[i];
					i++;
					j++;
					//parsed = 1;
				}
				i++;
				j=0;
				while (j<3)
				{
					angle[j]=str[i];
					i++;
					j++;
					//parsed = 1;
				}
				//printf ("posx %s posy %s\n",posx, posy);
				displaySprite(screen, SDL_DisplayFormat(tmp2), atoi(posx)-(TRIANGLE_WIDTH/2), atoi(posy)-(TRIANGLE_WIDTH/2));
				i++;
			}
			/****************  Flags ***********************/
			if (str[i]=='f')
			{
				//printf("I'v got a flag\n");
				i+=2;
				j =0;
				while (j<3)
				{
					posx[j]=str[i];
					i++;
					j++;
					//parsed = 1;
				}
				i++;
				j=0;
				while (j<3)
				{
					posy[j]=str[i];
					i++;
					j++;
					//parsed = 1;
				}
				//printf ("posx %d posy %d\n", atoi(posx), atoi(posy));
				i++;
				}
				if (str[i]=='*')
				{
					//printf("parsed!!\n");
					parsed=1;
				}
				i++;
			//parsed=1;
			
		}
		//drawPlayer(player);
		free(key);
		free(posx);
		free(posy);
		free(angle);
	}
}

